package com.xinya.wanandroidclient.ui.me

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import androidx.databinding.ViewDataBinding
import com.xinya.wanandroidclient.R
import com.xinya.wanandroidclient.base.BaseFragment
import com.xinya.wanandroidclient.common.setNoRepeatClick
import com.xinya.wanandroidclient.databinding.FragmentMeBinding
import com.xinya.wanandroidclient.eventbus.observer.LoginMessageObserver
import com.xinya.wanandroidclient.eventbus.observer.LogoutMessageObserver
import com.xinya.wanandroidclient.utils.UserSPUtils
import com.xinya.wanandroidclient.view.NavHostFragment

/**
 *
 * @ClassName:      MeFragment
 * @Description:     java类作用描述
 * @Author:         mhzhuxinya@163.com
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */
class MeFragment : BaseFragment() {

    companion object {
        private const val TAG = "MeFragment"
    }

    private val viewModel by lazy {
        getFragmentVieModel(MeViewModel::class.java)
    }

    private val loginMessageObserver by lazy {
        LoginMessageObserver()
    }

    private val logoutMessageObserver by lazy {
        LogoutMessageObserver()
    }

    override fun getLayoutId() = R.layout.fragment_me

    @SuppressLint("SetTextI18n")
    override fun init(savedInstanceState: Bundle?) {

        // 绑定eventBUs
        lifecycle.addObserver(loginMessageObserver)
        lifecycle.addObserver(logoutMessageObserver)

        dataBinding.apply {
            val userInfo = UserSPUtils.INST.getUserInfo()
            if (TextUtils.isEmpty(userInfo.username)) {
                return
            }
            setLoginView(userInfo.username, userInfo.userId, userInfo.coinCount, userInfo.rank)
        }
    }

    private lateinit var dataBinding: FragmentMeBinding
    override fun initDataBinding(binding: ViewDataBinding?) {
        dataBinding = binding as FragmentMeBinding
    }

    @SuppressLint("SetTextI18n")
    override fun observer() {

        viewModel.apply {
            userInfoLiveData.observe(this@MeFragment) {
                setLoginView(it.username, it.userId, it.coinCount, it.rank)
            }

            errorLiveData.observe(this@MeFragment) {

            }
        }.getUserInfo()

        loginMessageObserver.setMessageEvent {
            // 接收到登录广播
            // 刷新页面
            Log.d(TAG, "onLoginMessageEvent: 登陆成功")
            setLoginView(it.user.username, it.user.id)
            // 请求用户信息
            viewModel.getUserInfo()
        }

        logoutMessageObserver.setMessageEvent {
            setLogoutView()
            Toast.makeText(context, "退出成功", Toast.LENGTH_SHORT).show()
        }
    }

    @SuppressLint("RestrictedApi")
    override fun onClick() {
        dataBinding.apply {
            tvLogin.setNoRepeatClick {
                // 登录按钮
                if (UserSPUtils.INST.isLogin()) {
                    return@setNoRepeatClick
                }
                NavHostFragment.findNavController(this@MeFragment)
                    .navigate(R.id.action_main_to_login)
                Log.d(TAG, "onClick: tvLogin")
            }

            ivIcon.setOnClickListener {
                // 点击头像
                Log.d(TAG, "onClick: ivIcon")
            }

            score.setOnClickListener {
                // 我的积分
                Log.d(TAG, "onClick: score")
            }

            favorite.setOnClickListener {
                // 我的收藏
                Log.d(TAG, "onClick: favorite")
            }

            share.setOnClickListener {
                // 我的分享
                Log.d(TAG, "onClick: share")
            }

            article.setOnClickListener {
                // 我的文章
                Log.d(TAG, "onClick: article")
            }

            setting.setOnClickListener {
                // 设置
                Log.d(TAG, "onClick: setting")
            }

            exit.setNoRepeatClick {
                // 退出
                // TODO: 2021/3/10 弹出Dialog让用户再次确认
                if (!UserSPUtils.INST.isLogin()) {
                    Toast.makeText(context, "尚未登录", Toast.LENGTH_SHORT).show()
                    return@setNoRepeatClick
                }
                viewModel.logout()
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun setLoginView(userName: String, userId: Int, coinCount: Int = 0, rank: Int = 0) {
        dataBinding.apply {
            tvLogin.text = userName
            tvScore.text = resources.getString(R.string.score_text) + coinCount.toString()
            tvRank.text = resources.getString(R.string.ranking_text) + rank.toString()
            tvId.text = resources.getString(R.string.id_info_text) + userId.toString()
        }
    }

    private fun setLogoutView() {
        dataBinding.apply {
            tvLogin.text = resources.getText(R.string.login_info_text)
            tvScore.text = resources.getString(R.string.score_text)
            tvRank.text = resources.getString(R.string.ranking_text)
            tvId.text = resources.getString(R.string.id_info_text)
        }
    }
}